<template>
  <div
    :class="className"
    :style="{ height: height, width: width }"
    ref="chartDom"
  ></div>
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons");

export default {
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
    fsData: {
      type: Array,
      default: () => [],
      required: true
    },
    glData: {
      type: Array,
      default: () => [],
      required: true
    },
    fsglDataX: {
      type: Array,
      default: () => [],
      required: true
    }
  },
  data() {
    return {
      chart: null,
      chartOptions: {
        legend: {
          data: ['功率', '风速'],
          textStyle: { color: "#ffffff" },
          right: 10,
          top: 0,
          itemHeight: 12,
          itemWidth: 12,
          itemGap: 20
        },
        grid: {
          top: 40,
          left: 40,
          right: 20,
          bottom: 30,
        },
        xAxis: {
          type: "category",
          data: [],
          axisLine: { lineStyle: { color: '#B0D7FF' } },
          axisTick: { show: false },
          axisLabel: { color: '#B0D7FF' }
        },
        yAxis: [
          {
            type: "value",
            name: '功率',
            nameTextStyle: { color: '#49F0FF', padding: [0, 0, 0, 40] },
            splitLine: { show: false },
            axisLine: {
              show: true,
              lineStyle: { color: '#49F0FF' }
            },
            // 添加格式化显示
            axisLabel: {
              formatter: function(value) {
                // 将数值除以10000并添加单位
                return value / 10000 ;
              }
            },
            axisLabel: { color: '#49F0FF' }
          },
          {
            type: "value",
            name: '风速',
            nameTextStyle: { color: '#F7CB5A', padding: [0, 0, 0, 40] },
            splitLine: { show: false },
            axisLine: {
              show: true,
              lineStyle: { color: '#F7CB5A' }
            },
            // 添加格式化显示
            axisLabel: {
              formatter: function(value) {
                // 将数值除以10000并添加单位
                return value / 10000 ;
              }
            },
            axisLabel: { color: '#F7CB5A' }
          }
        ]
      }
    };
  },
  watch: {
    // 深度监听数据变化
    fsData: {
      handler(newVal) {
        this.updateChartData();
      },
      deep: true
    },
    glData: {
      handler(newVal) {
        this.updateChartData();
      },
      deep: true
    },
    fsglDataX: {
      handler(newVal) {
        this.updateChartData();
      },
      deep: true
    }
  },
  mounted() {
    this.initChart();
    window.addEventListener('resize', this.handleResize);
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose();
      this.chart = null;
    }
    window.removeEventListener('resize', this.handleResize);
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$refs.chartDom, "macarons");
      this.updateChartData();
    },
    updateChartData() {
      if (!this.chart){
        return;
      }
      this.chartOptions.xAxis.data = this.fsglDataX
      const options = {
        ...this.chartOptions,
        series: [
          {
            name: "功率",
            data: this.glData,
            type: "line",
            symbol: 'circle',
            symbolSize: 6,
            lineStyle: { width: 2, color: '#49F0FF' },
            itemStyle: { color: '#49F0FF' },
            areaStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: 'rgba(73, 240, 255, 0.3)' },
                { offset: 1, color: 'rgba(73, 240, 255, 0)' }
              ])
            },
            yAxisIndex: 0
          },
          {
            name: "风速",
            data: this.fsData,
            type: "line",
            symbol: 'circle',
            symbolSize: 6,
            lineStyle: { width: 2, color: '#F7CB5A' },
            itemStyle: { color: '#F7CB5A' },
            areaStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: 'rgba(247, 203, 90, 0.3)' },
                { offset: 1, color: 'rgba(247, 203, 90, 0)' }
              ])
            },
            yAxisIndex: 1
          }
        ],
        tooltip: {
          trigger: 'axis',
          axisPointer: { type: 'cross' },
          formatter: params => {
            return `
              <div>时间: ${params[0].axisValue}</div>
              <div>功率: ${params[0].data} kW</div>
              <div>风速: ${params[1].data} m/s</div>
            `;
          }
        }
      };

      this.chart.setOption(options, true);
      this.chart.resize();
    },
    handleResize() {
      if (this.chart) {
        this.chart.resize();
      }
    }
  }
};
</script>

<style scoped>
.chart {
  background: transparent;
}
</style>
